﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZK.Common;
using ZK.HardWareHelp;
using System.Threading;
using System.Timers;

namespace ZK.YCGLRD_TransManage.UC
{
    public partial class InOut : UserControl
    {
        private HardWareHelp.MW_102N_CardReader mW_102N_CardReader;
        private WFAutoSizeHelper wFAutoSizeHelper = new WFAutoSizeHelper();
        /// <summary>
        /// 当前车牌
        /// </summary>
        private string nowCardNum = "";
        static object obj;

        private System.Timers.Timer timer = new System.Timers.Timer(Convert.ToDouble(Common.AppConfigHelper.GetAppSettings("TimerTime"))); //设置时间间隔为5秒
        /// <summary>
        /// 运销车辆信息
        /// </summary>
        private Models.YxYsTransportReturnDto yxYsTransportReturnDto;

        /// <summary>
        /// 构造函数
        /// </summary>
        public InOut()
        {
            InitializeComponent();
            Common.DelegateHelper.MW_102N_MessageReceivedEvent += MW_102N_MessageReceived;
            MyInit();
            InitReader();
            InitLed();


            InitTimer();

            obj = new object();
        }
        /// <summary>
        /// 初始化定时器
        /// </summary>
        private void InitTimer()
        {
            timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_TimesUp);
            timer.AutoReset = false; //
            timer.Enabled = true;
            timer.Start();
        }

        /// <summary>
        /// 定时器的委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer_TimesUp(object sender, ElapsedEventArgs e)
        {
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 初始化led
        /// </summary>
        private void InitLed()
        {
            int a = OnbonBx_sdk_dual.InitSdk();
            ShowLed(Com.TipsStringHelper.gldchyn);
        }

        /// <summary>
        /// led显示消息
        /// </summary>
        /// <param name="str"></param>
        private void ShowLed(string str)
        {

            HaredWareCommonM ledQ = IConfig.GetSetLedDC;
            Common.TestConnect.CheckDeviceStatus(ledQ.Ip, ledQ.Name);
            OnbonBx_sdk_dual.dynamicArea_6(str, ledQ.Ip);


        }

        /// <summary>
        /// led,弹窗同时显示信息
        /// </summary>
        /// <param name="str"></param>
        private void LedMessageBoxShow(string str)
        {
            ShowLed(str);
            Common.MessageDialog.MessageBoxShow(str, Com.TipsStringHelper.yccw);
        }

        /// <summary>
        /// 提示信息现在在led，页面上。
        /// </summary>
        /// <param name="str"></param>
        private void ledNowMessageShow(string str)
        {
            ShowLed(str);
            NowMessagesShow(str);
        }

        /// <summary>
        /// 提示信息显示在页面上
        /// </summary>
        /// <param name="str"></param>
        private void NowMessagesShow(string str)
        {

            try
            {
                this.Invoke(new Action(() =>
               {
                   labelNowMessage.Text = str;
               }));
            }
            catch (Exception ex)
            {

                Common.Log4.LogManage.WriteErr(ex.ToString());
            }
        }

        /// <summary>
        /// 初始化读卡器
        /// </summary>
        private void InitReader()
        {
            mW_102N_CardReader = new HardWareHelp.MW_102N_CardReader("DC");
        }

        /// <summary>
        /// 初始化 
        /// </summary>
        private void MyInit()
        {
            wFAutoSizeHelper.GetAllInitInfo(this.Controls[0], this);
            Common.DelegateHelper.SendWidthHeightEvent += SendWidthHeight;
        }

        /// <summary>
        /// 窗口变化委托事件
        /// </summary>
        /// <param name="Width"></param>
        /// <param name="height"></param>
        private void SendWidthHeight(int Width, int height)
        {
            if (wFAutoSizeHelper.ControlsInfo.Count > 0)//如果字典中有数据，即窗体改变
            {
                wFAutoSizeHelper.ControlsChangeInit(Width, height);//表示pannel控件
                wFAutoSizeHelper.ControlsChange(this.Controls[0]);
            }
        }

        /// <summary>
        /// 初始化室外读卡器
        /// </summary>
        /// <param name="cardNum"></param>
        private void MW_102N_MessageReceived(string cardNum)
        {
            try
            {
                if (nowCardNum != cardNum)
                {
                    if (timer.Enabled)
                    {
                        ledNowMessageShow(Com.TipsStringHelper.sjjgtdshzs);
                        ClearTextTime();
                        return;
                    }
                    timer.Start();
                    nowCardNum = cardNum;
                    this.Invoke(new Action(() =>
                    {
                        textBoxCardNum.Text = cardNum;
                    }));
                    var YxInfoRes = Com.WebApiService.GetTruckYxInfo(cardNum);
                    if (YxInfoRes.data == null)
                    {
                        ledNowMessageShow(YxInfoRes.msg);
                        ClearTextTime();
                        return;
                    }
                    yxYsTransportReturnDto = YxInfoRes.data;
                    SetText();
                    var resM = Com.WebApiService.AutoIntoExit(new Models.AutoIntoExitSendM()
                    {
                        truckNo = "",
                        cardNo = cardNum
                    });

                    ledNowMessageShow(resM.msg);
                    ClearTextTime();
                }
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("MW_102N_MessageReceived " + ex.ToString());
                ledNowMessageShow(Com.TipsStringHelper.hqsjyc);
                ClearTextTime();
            }



        }

        /// <summary>
        /// 清空页面数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonClear_Click(object sender, EventArgs e)
        {
            ClearTextTime();
            //  ClearText();
        }

        /// <summary>
        /// 出厂事件按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonOut_Click(object sender, EventArgs e)
        {
            string cardNum = textBoxCardNum.Text.Trim();
            //出厂接口
            var inRes = Com.WebApiService.TruckExit(
                new Models.ExitDtoSendM()
                {
                    cardNo = cardNum,
                }
                );
            ledNowMessageShow(inRes.msg);
            ClearTextTime();
        }

        /// <summary>
        /// 入厂按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonInt_Click(object sender, EventArgs e)
        {
            string cardNum = textBoxCardNum.Text.Trim();
            //入厂接口
            var inRes = Com.WebApiService.TruckAdmission(
                new Models.AdmissionDtoSendM()
                {
                    cardNo = cardNum,
                }
                );
            ledNowMessageShow(inRes.msg);
            ClearTextTime();

        }

        /// <summary>
        /// 间隔时间清空数据
        /// </summary>
        private void ClearTextTime()
        {
            Task.Factory.StartNew(new Action(() =>
            {
                Thread.Sleep(Convert.ToInt32(Common.AppConfigHelper.GetAppSettings("InOutClearTextTime")));
                ClearText();
            }));
        }

        /// <summary>
        /// 设置页面数据
        /// </summary>
        private void SetText()
        {
            try
            {
                this.Invoke(new Action(() =>
                   {
                       textBoxKZT.Text = Enum.GetName(typeof(SysDefinitionEnum.SendType), yxYsTransportReturnDto.sendType);
                       textBoxCYS.Text = yxYsTransportReturnDto.carrierName;
                       textBoxGSMC.Text = yxYsTransportReturnDto.shipperName;
                       textBoxHWMC.Text = yxYsTransportReturnDto.cargoName;
                       textBoxBZ.Text = yxYsTransportReturnDto.memo;
                       textBoxLX.Text = Enum.GetName(typeof(SysDefinitionEnum.TruckState), yxYsTransportReturnDto.truckState);
                   }));
            }
            catch (Exception ex)
            {

                Common.Log4.LogManage.WriteErr(ex.ToString());
            }
        }

        /// <summary>
        /// 清空页面数据
        /// </summary>
        private void ClearText()
        {
            try
            {
                this.Invoke(new Action(() =>
                   {
                       textBoxKZT.Text = "";
                       textBoxCYS.Text = "";
                       textBoxGSMC.Text = "";
                       textBoxHWMC.Text = "";
                       textBoxBZ.Text = "";
                       textBoxLX.Text = "";
                       textBoxCardNum.Text = "";
                   }));
            }
            catch (Exception ex)
            {

                Common.Log4.LogManage.WriteErr(ex.ToString());
            }
            nowCardNum = "";
            ledNowMessageShow(Com.TipsStringHelper.gldchyn);
        }

        private void labelCompanyName_Click(object sender, EventArgs e)
        {

        }
    }
}
